package com.xb.loan.util;

import java.io.*;

/**
 * Created by wangz on 14-12-24.
 */
public class SerializeUtil {


    /**
     * 对象转数组
     */

    public static byte[] serialize(Object obj) {

        try {
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
                 ObjectOutputStream  oos = new ObjectOutputStream(bos);

            ){
                oos.writeObject(obj);
                return bos.toByteArray ();
            }
        } catch (IOException e) {
            throw new RuntimeException("serialize error!",e);
        }
    }


    public static Object unserialize(byte[] bytes) {


        try {
            try (ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
                 ObjectInputStream ois = new ObjectInputStream(bis);

            ){
                return ois.readObject();
            }
        } catch (Exception e) {
            throw new RuntimeException("unserialize error!",e);

        }
    }
}
